package com.qianbei.common.base;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.IBinder;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.inputmethod.InputMethodManager;
import android.widget.AdapterView;

import com.qianbei.R;
import com.qianbei.common.net.control.BaseNetActivity;
import com.qianbei.common.net.control.ServerControl;
import com.qianbei.common.view.title.TitleFragment;

import butterknife.ButterKnife;

public class BaseActivity extends BaseNetActivity implements BaseFragmentClick,
        OnClickListener {

    protected TitleFragment mTitle;
    public boolean isActive;

    protected String tag;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        ActivityManagers.getInstance().pushStack(this);
        super.onCreate(savedInstanceState);
        tag = this.getClass().toString();
    }

    @Override
    protected void onResume() {
        super.onResume();
        isActive = true;
    }

    @Override
    protected void onPause() {
        super.onPause();
        isActive = false;
    }

    @Override
    public void setContentView(int layoutResID) {
        // getWindow().setSoftInputMode(
        // WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
        super.setContentView(layoutResID);
        mContext = this;
        mTitle = (TitleFragment) getSupportFragmentManager().findFragmentById(
                R.id.title_fragment);

        initView();
        initClick();
    }

    @Override
    public void setContentView(View view) {
        super.setContentView(view);
        mContext = this;
        mTitle = (TitleFragment) getSupportFragmentManager().findFragmentById(
                R.id.title_fragment);

        initView();
        initClick();
    }

    public void initView() {
    }

    public void initClick() {
    }

    public void setTitle(String text) {
        if (mTitle != null) {
            mTitle.setTitle(text);
        }
    }
    public void setLeftGone(){
        if (mTitle != null) {
            mTitle.setLefeGone();
        }

    }


    public void setTitle(int titleId) {
        if (mTitle != null) {
            mTitle.setTitle(getString(titleId));
        }
    }

    /**
     * 显示替换fragment
     */
    protected void showFragment(Fragment fg, int contaner) {
        FragmentManager fm = getSupportFragmentManager();
        FragmentTransaction transaction = fm.beginTransaction();
        if (!fg.isAdded()) {
            transaction.replace(contaner, fg);
        } else {
            transaction.show(fg);
        }
        transaction.commitAllowingStateLoss();
        // 重置导航选中状态
    }


    protected void showFragmentreplace(Fragment fg, int contaner) {
        FragmentManager fm = getSupportFragmentManager();
        FragmentTransaction transaction = fm.beginTransaction();
        transaction.replace(contaner, fg);
        transaction.commitAllowingStateLoss();
        // 重置导航选中状态
    }

    protected void showFragmentBack(Fragment fg, int id, int contaner) {
        FragmentManager fm = getSupportFragmentManager();
        FragmentTransaction transaction = fm.beginTransaction();
        String tag = String.valueOf(id);
        if (null == fm.findFragmentByTag(tag)) {
            transaction.replace(contaner, fg, tag);
            transaction.addToBackStack(null);
        } else {
            transaction.show(fg);
        }
        transaction.commitAllowingStateLoss();
    }

    /**
     * 简化Activity启动方法
     *
     * @param targetClass
     */
    public void startActivity(Class<? extends BaseActivity> targetClass) {
        startActivity(new Intent(this, targetClass));
    }

    /**
     * 实现 baseFragment 的事件通知 ，子类如果需要处理 从写此方法 集中处理 不需要子类 实现此接口
     */
    @Override
    public void fClick(int id) {

        switch (id) {
            case R.id.title_left:
                hideSoftInputFromWindow();
                finish();
                break;

            default:
                break;
        }
    }

    protected void hideSoftInputFromWindow() {
        InputMethodManager im = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
        View curentView = getCurrentFocus();
        if (im != null && curentView != null) {
            IBinder iBinder = getCurrentFocus().getApplicationWindowToken();
            im.hideSoftInputFromWindow(iBinder,
                    InputMethodManager.HIDE_NOT_ALWAYS);
        }
    }

    @Override
    public void onClick(View v) {

    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position,
                            long id) {

    }

    // 点击屏幕 关闭输入弹出框
    @Override
    public boolean onTouchEvent(MotionEvent event) {

        hideSoftInputFromWindow();
        return super.onTouchEvent(event);
    }

    @Override
    public void finish() {
        ButterKnife.unbind(this);
        super.finish();
        ActivityManagers.getInstance().popStack(this);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        ActivityManagers.getInstance().popStack(this);
    }

    /**
     * activity之间的跳转
     *
     * @param target
     */
    public void jump(Class target) {
        startActivity(new Intent(this, target));
    }

    /**
     * activity之间的跳转
     *
     * @param intent
     */
    public void jump(Intent intent) {
        startActivity(intent);
    }

    public void setTitleWhitStyle() {
        mTitle.setTitleWhitStyle();
    }


    /***
     * 跳转到系统 浏览器 用于广告跳转
     * @param url
     */
    public void startAd(String url, String adId) {
        addAdClick(adId);
        Intent intent = new Intent();
        intent.setAction("android.intent.action.VIEW");
        Uri content_url = Uri.parse(url);
        intent.setData(content_url);
        mContext.startActivity(intent);
        ((BaseActivity) mContext).overridePendingTransition(R.anim.comm_right_in, R.anim.comm_right_out);
    }

    /***
     * 计算 广告点击
     */
    private void addAdClick(String adId) {
        new ServerControl(AD_ADD_CLICK, adId).startVolley();
    }
}
